package com.fuelwallet.model;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SearchedPlacesHistoryHandle {
	private SQLiteDatabase db;
	private SearchedHistorySQLiteHelper dbHelper;
	
	public SearchedPlacesHistoryHandle(Context context){
		dbHelper = new SearchedHistorySQLiteHelper(context);
	}
	
	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	public boolean putPlace(String place){
		boolean NameAvailable = true;
		List<String> NameList = this.getPlaceHistory();
		
		if(NameList.indexOf(place) != -1){
			NameAvailable = false;
		}else{
			db.execSQL("INSERT INTO " + SearchedHistorySQLiteHelper.TABLE_SEARCH_HISTORY + " ("
					+ SearchedHistorySQLiteHelper.COLUMN_PLACE + ")"
					+ " values (" + "\"" + place + "\")");
		}
		return NameAvailable;
	}
	
	public void putPlaces(ArrayList<String> list){
		for(String place: list){
			putPlace(place);
		}		
	}
	
	public List<String> getPlaceHistory(){
		List<String> NameList = new ArrayList<String>();
		String[] args = { };
		
		Cursor result = db.rawQuery("SELECT " + SearchedHistorySQLiteHelper.COLUMN_PLACE 
				+ " FROM " + SearchedHistorySQLiteHelper.TABLE_SEARCH_HISTORY, args);
		
		result.moveToFirst();
		while (!result.isAfterLast()) {
			NameList.add(result.getString(0));
			result.moveToNext();
		}
		result.close();
		return NameList;
	}
	
	public void clearPlaceHistory(){
		db.execSQL("DELETE FROM " + SearchedHistorySQLiteHelper.TABLE_SEARCH_HISTORY);
		db.execSQL("VACUUM");
	}
	
	private class SearchedHistorySQLiteHelper extends SQLiteOpenHelper {
		public static final String TABLE_SEARCH_HISTORY = "search_history";
		public static final String COLUMN_ID = "_id";
		public static final String COLUMN_PLACE = "place_name";
		
		private static final String DATABASE_NAME = "search_history.db";
		private static final int DATABASE_VERSION = 1;
		
		private static final String DATABASE_CREATE = "create table "
				+ TABLE_SEARCH_HISTORY + "( " 
				+ COLUMN_ID + " integer primary key autoincrement, " 
				+ COLUMN_PLACE + " text not null);";
		
		public SearchedHistorySQLiteHelper(Context context){
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);				
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(SearchedHistorySQLiteHelper.class.getName(),
					"Upgrading database from version " + oldVersion + " to "
							+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS" + TABLE_SEARCH_HISTORY);
			db.execSQL(DATABASE_CREATE);				
		}
		
	}
}
